package J5_3;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;

public class test {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        char[] ch = str.toCharArray();
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < ch.length; i++) {
            if ((ch[i] - '0') % 2 == 0) {
                ret.append("0");
            } else {
                ret.append("1");
            }
        }
        System.out.println(Integer.parseInt(ret.toString()));
    }

    public static void main2(String[] args) {
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        long[][] arr = new long[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                arr[i][j] = in.nextLong();
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                arr[0][j] += arr[i][j];
                arr[i][0] += arr[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                long ret = arr[i+1][0] + arr[0][j+1] - arr[i+1][j+1];
                out.print(ret + " ");
            }
            out.println();
        }
        out.close();
    }

    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        int[][] arr = new int[2][n];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        long sumH = 0, sumS = 0;
        for (int i = 0; i < k; i++) {
            sumH += arr[0][i];
            sumS += arr[1][i];
        }
        long maxH = sumH, minS = sumS;
        int ret = 1;
        for (int l = 0, r = k; r < arr[0].length; l++,r++) {
            sumH -= arr[0][l];
            sumH += arr[0][r];
            sumS -= arr[1][l];
            sumS += arr[1][r];
            if (sumH > maxH || sumH == maxH && sumS < minS) {
                ret = l + 2;
                maxH = sumH;
                minS = sumS;
            }
        }
        System.out.println(ret);
    }


    public String compressString (String param) {
        char[] ch = param.toCharArray();
        StringBuilder ret =new StringBuilder();
        if (ch.length == 0) {
            return ret.toString();
        }
        ret.append(ch[0]);
        char prev = ch[0];
        for (int i = 1; i < ch.length; i++) {
            int count = 1;
            while (i < ch.length - 1 && prev == ch[i]) {
                count++;
                i++;
            }
            if (i == ch.length - 1 && ch[i] == prev) {
                count++;
                ret.append(count);
                continue;
            }
            prev = ch[i];
            if (count > 1) {
                ret.append(count);
            }
            ret.append(ch[i]);
        }
        return ret.toString();
    }

    public static void main4(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        int[][] arr = new int[n][2];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < n; j++) {
                arr[j][i] = in.nextInt();
            }
        }
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        list.sort((a ,b) -> {
            if (b[1] == a[1]) {
                return a[0] - b[0];
             }
            return b[1] - a[1];
        });
        long ret1 = 0, ret2 = 0;
        for (int i = 0; i < k; i++) {
            ret1 += list.get(i)[0];
            ret2 += list.get(i)[1];
        }
        System.out.println(ret1 + " " + ret2);
    }

    public int knapsack1 (int V, int n, int[][] vw) {
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(vw[i]);
        }
        list.sort((a, b) -> {
            double v1 = a[0], w1 = a[1];
            double v2 = b[0], w2 = b[1];
            return (int) (w2/v2 - w1/v1);
        });
        int res = 0;
        for (int i = 0; i < n; i++) {
            if (list.get(i)[0] <= V) {
                res += list.get(i)[1];
                V -= list.get(i)[0];
            }
            if (V == 0) {
                return res;
            }
        }
        return res;
    }
    public static int knapsack (int V, int n, int[][] vw) {
        int[][] dp = new int[n + 1][V + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= V; j++) {
                if (vw[i - 1][0] <= j) {
                    dp[i][j] = Math.max(dp[i-1][j - vw[i - 1][0]] + vw[i - 1][1], dp[i-1][j]);
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[n][V];
    }

    public static void main(String[] args) {
        int[] arr1 = new int[]{1,3};
        int[] arr2 = new int[]{10,4};
        int[][] arr = new int[][]{arr1, arr2};
        knapsack(10,2,arr);
    }
}
